<template>
    <LModalForm 
        :visible="visible"
        :confirmLoading="confirmLoading" 
        :title="title" 
        ref="attribute" 
        :formAttributes="formAttributes" 
        @handleOk="handleOk" 
        @handleCancel="handleCancel"
        @clickEventHandler="clickEventHandler"
        @changeEventHandler="changeEventHandler"></LModalForm>

</template>

<script>
import LModalForm from "@/components/lt/form/LModalForm"
import pageSetting from '@/components/lt/codeGenerator/pageSetting'
import pick from 'lodash.pick'

export default {
    name:'InputNumberAttribute',
    components:{
        LModalForm
    },
    data(){
        return{
            pageSetting,
            title:'详情',
            visible:false,
            confirmLoading: false,
            model:{},
            formAttributes:{
              type:'',//collapse,tabs
              formItems:[
                {
                  title:'标签一',
                  columns:1,
                  fields:[
                    {field:'component',label:'组件类型',component:pageSetting.常量.表单组件类型.下拉选择,options:pageSetting.数据字典.表单类型,rules: [{ required: true, message: '不能为空!' }]},
                    {field:'label',label:'标题',component:pageSetting.常量.表单组件类型.单行文本,rules: [{ required: true, message: '不能为空!' }]},
                    {field:'field',label:'字段代码',component:pageSetting.常量.表单组件类型.单行文本,disabled:true,rules: [{ required: true, message: '不能为空!' }]},
                    {field:'entityClassName',label:'实体名称',component:pageSetting.常量.表单组件类型.单行文本,disabled:true},
                    {field:'dbField',label:'数据库字段',component:pageSetting.常量.表单组件类型.单行文本,disabled:true},
                    {field:'disabled',label:'是否禁用',component:pageSetting.常量.表单组件类型.开关,checkedChildren:true,unCheckedChildren:false,rules: [{ required: true, message: '不能为空!' }]},
                    {field:'hide',label:'是否隐藏',component:pageSetting.常量.表单组件类型.开关,checkedChildren:true,unCheckedChildren:false,rules: [{ required: true, message: '不能为空!' }]},
                    {field:'initialValue',label:'默认值',component:pageSetting.常量.表单组件类型.单行文本},
                    {field:'required',label:'是否必填',component:pageSetting.常量.表单组件类型.开关,checkedChildren:true,unCheckedChildren:false,rules: [{ required: true, message: '不能为空!' }]},
                    {field:'step',label:'改变步数',component:pageSetting.常量.表单组件类型.数字输入框,initialValue:1},
                    {field:'min',label:'最大值',component:pageSetting.常量.表单组件类型.数字输入框},
                    {field:'max',label:'最小值',component:pageSetting.常量.表单组件类型.数字输入框},
                    {field:'rules',label:'自定义校验(JSON数组)',component:pageSetting.常量.表单组件类型.多行文本},
                    {field:'labelCol',label:'标题占列数',component:pageSetting.常量.表单组件类型.下拉选择,options:pageSetting.数据字典.栅格占位格数},
                    {field:'wrapperCol',label:'内容占列数',component:pageSetting.常量.表单组件类型.下拉选择,options:pageSetting.数据字典.栅格占位格数},
                 ]
                },
              ]

            }


        }
    },
    methods:{
       
        edit (record) {
            this.visible = true
            this.model=record
            this.$nextTick(() => {
                this.$refs.attribute.getForm().setFieldsValue(pick(this.model,'component','label','field','disabled','initialValue','required','rules','step','min','max','entityClassName','dbField','hide','labelCol','wrapperCol'))
            })
            
        },
        handleOk(){
            this.$refs.attribute.getForm().validateFields((err, values) => {
                if (!err) {
                    this.model = Object.assign(this.model, values)
                    this.setRules()
                    this.close()
                }
            })
            
        },
        handleCancel(){
            this.visible=false
        },
        close () {
            this.$emit('close');
            this.visible = false;
        },
        changeEventHandler(changeItem){
            if(changeItem.hasOwnProperty('component')){
                this.model.component = changeItem.component
                this.visible=false
                this.$emit('fieldEdit',this.model)
            }
        },
        clickEventHandler(field,form){
            
        },
        setRules(){
            if(this.model["rules"]){
                let rules = JSON.parse(this.model["rules"])
                let flag = true
                for (let item of rules) {
                    if('required' in item){
                        item['required']=this.model["required"]
                        flag =false
                        break
                    }
                }
                if(flag){
                    rules.push({ required: this.model["required"], message: '不能为空!' })
                }
                this.model["rules"] = JSON.stringify(rules)
            }else{
                this.model["rules"] = JSON.stringify([{ required: this.model["required"], message: '不能为空!' }])
            }
        },
    },
    created(){
        
    },
}
</script>